package com.weilele.leisure.time.ui.module.video_cache

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.exoplayer2.offline.Download
import com.weilele.base.library.BaseBindingFragment
import com.weilele.base.library.SwitchView
import com.weilele.leisure.time.R
import com.weilele.leisure.time.VIDEO_ID_SD_CARD
import com.weilele.leisure.time.bean.db.HistoryVideoBean
import com.weilele.leisure.time.databinding.FragmentVideoCacheBinding
import com.weilele.leisure.time.ui.holder.VideoCacheHolder
import com.weilele.mvvm.adapter.refreshList
import com.weilele.mvvm.utils.activity.getResString
import com.weilele.mvvm.utils.activity.navigateTo
import com.weilele.mvvm.utils.formJsonString
import com.weilele.mvvm.utils.thread.LoopTask
import com.yizisu.playerlibrary.service.download.PlayerDownload

class VideoCacheFragment : BaseBindingFragment<FragmentVideoCacheBinding>() {
    companion object {
        fun start(appCompatActivity: AppCompatActivity) {
            appCompatActivity.navigateTo<VideoCacheActivity>()
        }

        fun getDownloadByVideoId(
            videoId: Int?,
            datas: MutableList<Download>
        ): MutableList<Download> {
            val id = videoId ?: return mutableListOf()
            return datas.filter {
                String(it.request.data).formJsonString<HistoryVideoBean>().videoId == id
            }.toMutableList()
        }

        private fun groupByDownloads(datas: MutableList<Download>): MutableList<Pair<HistoryVideoBean, List<Download>>> {
            val maps = mutableMapOf<Int?, HistoryVideoBean>()
            val results = datas.groupBy {
                val bean = String(it.request.data).formJsonString<HistoryVideoBean>()
                val id = bean.videoId
                maps[id] = bean
                id
            }
            val list = mutableListOf<Pair<HistoryVideoBean, List<Download>>>()
            for ((id, values) in results) {
                maps[id]?.let {
                    list.add(Pair(it, values))
                }
            }
            list.add(Pair(HistoryVideoBean().apply {
                videoId = VIDEO_ID_SD_CARD
                title = getResString(R.string.local_video).toString()
            }, mutableListOf()))
            return list
        }
    }

    private val videoCacheList
        get() = groupByDownloads(PlayerDownload.getAllDownloads())

    private val rcvAdapter = VideoCacheHolder.toAdapter()

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.rcv.adapter = rcvAdapter
        setTitle(getString(R.string.download))
        LoopTask.startWithLifecycleOwner(this, 1000) {
            refreshDOwnload()
        }
    }

    private fun refreshDOwnload() {
        rcvAdapter.refreshList(videoCacheList)
        if (videoCacheList.isNullOrEmpty()) {
            showOtherView(
                SwitchView.OtherBean(
                    getResString(R.string.no_cache_hint).toString(),
                    R.drawable.icon_empty
                )
            )
        }
    }

    override fun isNeedSwitchView(): Boolean {
        return true
    }

    override fun isNeedToolbar(): Boolean {
        return true
    }
}